package com.info.zhiduoduo.repository.mapper.marketingcooperative;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.marketingcooperative.MarketingCooperativePartnerDto;
import com.info.zhiduoduo.repository.entity.marketingcooperative.MarketingCooperative;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface MarketingCooperativeMapper extends BaseMapper<MarketingCooperative> {

	@Select(" select * from zdd_marketing_cooperative where compid = #{compid} and partnerid = #{partnerid}")
	MarketingCooperative getByCompidAndPartnerid(int compid, Integer partnerid);

	@Select(" select mc.*,pa.realname as partnerName,p.phone as partnerPhone,c.compname,pa.promo_code, " +
			" inv.partnerid as inviter,inv.realname as inviterName,inv.promo_code as inviterCode " +
			" from zdd_marketing_cooperative mc " +
			" join zdd_company c on c.compid = mc.compid " +
			" join zdd_partner_account pa on pa.partnerid = mc.partnerid " +
			" join zdd_partner p on p.partnerid = mc.partnerid " +
			" left join zdd_partner_account inv on inv.promo_code = p.invite_code and p.invite_code != 0 " +
			" where mc.partnerid = #{partnerid} " +
			" order by mc.id desc")
	List<MarketingCooperativePartnerDto> listByPartnerid(int partnerid);

	@Select(" select mc.*,pa.realname as partnerName,p.phone as partnerPhone,c.compname,pa.promo_code,   " +
			" inv.partnerid as inviter,inv.realname as inviterName,inv.promo_code as inviterCode," +
			" zpb.bankname,zpb.open_acct_bank_name,zpb.acctno,pa.acctid" +
			" from zdd_marketing_cooperative mc    " +
			" join zdd_company c on c.compid = mc.compid    " +
			" join zdd_partner_account pa on pa.partnerid = mc.partnerid    " +
			" join zdd_partner p on p.partnerid = mc.partnerid    " +
			" left join zdd_partner_account inv on inv.promo_code = p.invite_code and p.invite_code != 0 " +
			" LEFT JOIN zdd_partner_bankcard zpb ON zpb.partnerid = p.partnerid    " +
			" where mc.compid = #{compid}    " +
			" and pa.realname like '%${name}%'    " +
			" and p.phone like '%${phone}%'    " +
			" and IF(ISNULL(#{realnameAuth}) or #{realnameAuth}=0,1=1,pa.realname_auth = #{realnameAuth})  " +
			" order by mc.id desc")
	List<MarketingCooperativePartnerDto> listByCompid(int compid, String name, String phone,int realnameAuth);

	@Select("select COUNT(mcr.id) " +
			"from zdd_marketing_cooperative_record mcr " +
			"WHERE mcr.state = 1  " +
			"AND mcr.compid = #{compid}")
	int getNeed(@Param("compid") int compid);
}
